---
title: "C#代码片段"
categories:
- dotnet
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org1c5deb0">C# Code Snippets</a>
<ul>
<li><a href="#orgb405b5a">判断int型是否为枚举的成员</a></li>
<li><a href="#org4e10e7d">获取完整路径（能够获取iis虚拟路径）</a></li>
<li><a href="#org8e03148">图片缩放</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org1c5deb0">
<h2 id="org1c5deb0">C# Code Snippets</h2>
<div class="outline-text-2" id="text-org1c5deb0">
</div>
<div class="outline-3" id="outline-container-orgb405b5a">
<h3 id="orgb405b5a">判断int型是否为枚举的成员</h3>
<div class="outline-text-3" id="text-orgb405b5a">
<div class="org-src-container">
<pre class="src src-cs">// Define other methods and classes here
enum CommnetStatusType{
    type1,
    type2,
}

void Main()
{
	bool bvalue = System.Enum.IsDefined(typeof(CommnetStatusType), 1);
	bvalue.Dump();
}
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org4e10e7d">
<h3 id="org4e10e7d">获取完整路径（能够获取iis虚拟路径）</h3>
<div class="outline-text-3" id="text-org4e10e7d">
<blockquote>
<p>
<a href="http://stackoverflow.com/questions/6081433/getting-relative-virtual-path-from-physical-path">http://stackoverflow.com/questions/6081433/getting-relative-virtual-path-from-physical-path</a>
</p>
</blockquote>
<div class="org-src-container">
<pre class="src src-cs">System.Web.Hosting.HostingEnvironment.MapPath("/Data/");
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org8e03148">
<h3 id="org8e03148">图片缩放</h3>
<div class="outline-text-3" id="text-org8e03148">
<div class="org-src-container">
<pre class="src src-cs">protected Image GetSmallImg(Image image)
    {
	//获取原始高度和宽度
	int imageWidth = 0;
	int imageHeight = 0;
	if (image.Width &lt; 700)//返回原图
	{
	    return image;
	}
	imageHeight = 700 * image.Height / image.Width;
	imageWidth = 700;
	var bitmap = new Bitmap(imageWidth, imageHeight);
	// bitmap.SetResolution(1, 1);
	Image newImage = bitmap;
	using (Graphics g = Graphics.FromImage(newImage))
	{
	    //var s = g.DpiX;
	    //g.InterpolationMode = InterpolationMode.NearestNeighbor;
	    //g.SmoothingMode = SmoothingMode.None;
	    //g.InterpolationMode = InterpolationMode.NearestNeighbor;
	    //g.CompositingQuality = CompositingQuality.Invalid;
	    //g.PixelOffsetMode = PixelOffsetMode.None;
	    //  g.DrawImage(image, 0, 0, imageWidth,imageHeight);
	    g.DrawImage(image,
		new Rectangle(0, 0, imageWidth, imageHeight),
		new Rectangle(0, 0, image.Width, image.Height)
		, GraphicsUnit.Pixel);
	    g.Dispose();
	    return newImage;
	}
    }
    public static bool GetPicThumbnail(Image image, string outPath, int flag)
    {
	System.Drawing.Image iSource = image;
	ImageFormat tFormat = iSource.RawFormat;
	//以下代码为保存图片时，设置压缩质量  
	EncoderParameters ep = new EncoderParameters();
	long[] qy = new long[1];
	qy[0] = flag;//设置压缩的比例1-100  
	EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
	ep.Param[0] = eParam;
	try
	{
	    ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
	    ImageCodecInfo jpegICIinfo = null;
	    for (int x = 0; x &lt; arrayICI.Length; x++)
	    {
		if (arrayICI[x].FormatDescription.Equals("JPEG"))
		{
		    jpegICIinfo = arrayICI[x];
		    break;
		}
	    }
	    if (jpegICIinfo != null)
	    {
		iSource.Save(outPath, jpegICIinfo, ep);//dFile是压缩后的新路径  
	    }
	    else
	    {
		iSource.Save(outPath, tFormat);
	    }
	    return true;
	}
	catch
	{
	    return false;
	}
	finally
	{
	    iSource.Dispose();
	    iSource.Dispose();
	}
    }
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
